package com.woniuxy.controller;

import com.woniuxy.domain.DataGridView;
import com.woniuxy.domain.ResultDto;
import com.woniuxy.domain.RoleBean;
import com.woniuxy.pojo.SysRole;
import com.woniuxy.service.RoleService;
import com.woniuxy.service.UserService;
import com.woniuxy.util.Constants;
import com.woniuxy.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/sys")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @RequestMapping(value = "/toUserManager.html")
    public String toUserManager(){
        return "/sys/userList";
    }
    @RequestMapping(value = "/toAddUser.html")
    public String toAddUser(){
        return "/sys/userAdd";
    }
    @RequestMapping(value = "/toAssignRole.html")
    public String toAssignRole(){
        return "/sys/assignRole";
    }
    @RequestMapping(value = "/queryAllUser.html")
    @ResponseBody
    public DataGridView queryAllUser(UserVo userVo){
        return userService.getUserListByPage(userVo);
    }

    @RequestMapping(value = "/addUser.html")
    @ResponseBody
    public ResultDto addUser(UserVo userVo){
        ResultDto dto=new ResultDto();
        try {
            this.userService.addUser(userVo);
            dto.setCode(200);
            dto.setMsg(Constants.ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setCode(-1);
            dto.setMsg(Constants.ADD_FAILURE);
        }
        return dto;
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteUser.html")
    @ResponseBody
    public ResultDto deleteUser(Integer id){
        ResultDto dto=new ResultDto();
        try {
            this.userService.removeUser(id);
            dto.setCode(200);
            dto.setMsg(Constants.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setCode(-1);
            dto.setMsg(Constants.DELETE_FAILURE);
        }
        return dto;
    }
    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteBatchUser.html")
    @ResponseBody
    public ResultDto deleteBatchUser(@RequestParam("ids[]")Integer[] ids){
        ResultDto dto=new ResultDto();
        try {
            this.userService.removeBatchUser(ids);
            dto.setCode(200);
            dto.setMsg(Constants.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            dto.setCode(-1);
            dto.setMsg(Constants.DELETE_FAILURE);
        }
        return dto;
    }
    @RequestMapping(value = "/initRoleByUserId")
    @ResponseBody
    public DataGridView initRoleByUserId(Integer uid){
        //查询所有可用的角色
        List<SysRole> allRoleList=roleService.getAllAvailbleRoles();
        //查询当前用户所拥有的角色ID的集合
        List<Integer> currentUserRoleIds =roleService.queryUserRoleIdsByUid(uid);
        List<RoleBean> roleBeanList=new ArrayList<RoleBean>();
        for(SysRole role:allRoleList){
            RoleBean roleBean=new RoleBean();
            roleBean.setId(role.getId());
            roleBean.setAvailable(role.getAvailable());
            roleBean.setName(role.getName());
            roleBean.setRemarks(role.getRemarks());
            for(Integer rid:currentUserRoleIds){
                if(rid==role.getId()){
                    roleBean.LAY_CHECKED=true;
                    break;
                }
            }
            roleBeanList.add(roleBean);
        }
        return new DataGridView(Long.valueOf(roleBeanList.size()),roleBeanList);
    }
    @RequestMapping(value = "/assignRole.html")
    @ResponseBody
    public ResultDto assignRight(@RequestParam("rids[]")Integer[] rids,
                                  @RequestParam("uid") Integer uid){
        ResultDto resultDto=new ResultDto();
        try {
            this.userService.assignRole(rids,uid);
            resultDto.setCode(200);
            resultDto.setMsg(Constants.ASSIGN_ROLE_SUCCSS);
        } catch (Exception e) {
            e.printStackTrace();
            resultDto.setCode(-1);
            resultDto.setMsg(Constants.ASSIGN_ROLE_FAILURE);
        }
        return resultDto;
    }

}






















